<!DOCTYPE html>
<html lang="en-US" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>selenium基础 | 九问Blog</title>
    <meta name="description" content="欢迎访问九问Blog！分享一些Python全栈开发以及爬虫技术，希望对你有所帮助">
    <meta name="generator" content="VitePress v1.6.4">
    <link rel="preload stylesheet" href="/assets/style.DFMv0UyT.css" as="style">
    <link rel="preload stylesheet" href="/vp-icons.css" as="style">
    
    <script type="module" src="/assets/app.FY3_6mXQ.js"></script>
    <link rel="preload" href="/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/assets/chunks/theme.C5KTugK_.js">
    <link rel="modulepreload" href="/assets/chunks/framework.CKMM7nwK.js">
    <link rel="modulepreload" href="/assets/爬虫_selenium.md.BhDk9UyV.lean.js">
    <script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
    <script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
  </head>
  <body>
    <div id="app"><!--[--><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0b0ada53></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0b0ada53>Skip to content</a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar" data-v-ae24b3ad data-v-6aa21345><div class="wrapper" data-v-6aa21345><div class="container" data-v-6aa21345><div class="title" data-v-6aa21345><div class="VPNavBarTitle has-sidebar" data-v-6aa21345 data-v-1168a8e4><a class="title" href="/" data-v-1168a8e4><!--[--><!--]--><!----><span data-v-1168a8e4>九问Blog</span><!--[--><!--]--></a></div></div><div class="content" data-v-6aa21345><div class="content-body" data-v-6aa21345><!--[--><!--]--><div class="VPNavBarSearch search" data-v-6aa21345><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-6aa21345 data-v-dc692963><span id="main-nav-aria-label" class="visually-hidden" data-v-dc692963> Main Navigation </span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/%E5%B7%A5%E5%85%B7/Git/%E5%90%8C%E6%97%B6%E6%8E%A8%E9%80%81Gitee%E5%92%8CGitHub.html" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>工具</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Linux/Linux%E5%9F%BA%E7%A1%80%E5%85%A5%E9%97%A8.html" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Linux</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink active" href="/%E7%88%AC%E8%99%AB/%E5%AF%BC%E8%AE%BA.html" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>爬虫</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/%E6%B5%8B%E8%AF%95/faker%E6%95%B0%E6%8D%AE%E9%9A%8F%E6%9C%BA%E7%94%9F%E6%88%90.html" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>测试</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Python/%E5%8E%9F%E7%90%86%E7%B1%BB/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86.html" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Python</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Python%E6%A1%86%E6%9E%B6/Django/%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3.html" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Python框架</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/%E5%85%B6%E4%BB%96/%E5%85%AB%E8%82%A1/Linux.html" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>其他</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/%E5%89%8D%E7%AB%AF/Vue3/axios.html" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>前端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/LLM/LangChain/quick_start.html" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>LLM</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/docker/mysql.html" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>环境搭建</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-6aa21345 data-v-6c893767><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-6c893767 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-6aa21345 data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/jiuwen567" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/jiuwen567" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg t="1724307958358" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4234" width="200" height="200"><path d="M512 0C230.4 0 0 230.4 0 512s230.4 512 512 512 512-230.4 512-512S793.6 0 512 0z m284.8 313.6c0 12.8-12.8 25.6-25.6 25.6H416c-41.6 0-76.8 35.2-76.8 76.8v243.2c0 12.8 12.8 25.6 25.6 25.6h240c41.6 0 76.8-35.2 76.8-76.8v-12.8c0-12.8-12.8-25.6-25.6-25.6H480c-12.8 0-25.6-12.8-25.6-25.6v-64c0-12.8 12.8-25.6 25.6-25.6h291.2c12.8 0 25.6 12.8 25.6 25.6v144c0 92.8-76.8 169.6-169.6 169.6H252.8c-12.8 0-25.6-12.8-25.6-25.6V412.8C227.2 310.4 310.4 224 416 224h355.2c12.8 0 25.6 12.8 25.6 25.6v64z" fill="#B32225" p-id="4235"></path></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-6aa21345 data-v-bb2aa2f0 data-v-cf11d7a2><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-cf11d7a2><span class="vpi-more-horizontal icon" data-v-cf11d7a2></span></button><div class="menu" data-v-cf11d7a2><div class="VPMenu" data-v-cf11d7a2 data-v-b98bc113><!----><!--[--><!--[--><!----><div class="group" data-v-bb2aa2f0><div class="item appearance" data-v-bb2aa2f0><p class="label" data-v-bb2aa2f0>主题</p><div class="appearance-action" data-v-bb2aa2f0><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-bb2aa2f0 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div></div></div><div class="group" data-v-bb2aa2f0><div class="item social-links" data-v-bb2aa2f0><div class="VPSocialLinks social-links-list" data-v-bb2aa2f0 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/jiuwen567" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/jiuwen567" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg t="1724307958358" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4234" width="200" height="200"><path d="M512 0C230.4 0 0 230.4 0 512s230.4 512 512 512 512-230.4 512-512S793.6 0 512 0z m284.8 313.6c0 12.8-12.8 25.6-25.6 25.6H416c-41.6 0-76.8 35.2-76.8 76.8v243.2c0 12.8 12.8 25.6 25.6 25.6h240c41.6 0 76.8-35.2 76.8-76.8v-12.8c0-12.8-12.8-25.6-25.6-25.6H480c-12.8 0-25.6-12.8-25.6-25.6v-64c0-12.8 12.8-25.6 25.6-25.6h291.2c12.8 0 25.6 12.8 25.6 25.6v144c0 92.8-76.8 169.6-169.6 169.6H252.8c-12.8 0-25.6-12.8-25.6-25.6V412.8C227.2 310.4 310.4 224 416 224h355.2c12.8 0 25.6 12.8 25.6 25.6v64z" fill="#B32225" p-id="4235"></path></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-6aa21345 data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><div class="divider" data-v-6aa21345><div class="divider-line" data-v-6aa21345></div></div></div><!----></header><div class="VPLocalNav has-sidebar empty" data-v-5d98c3a5 data-v-a6f0e41e><div class="container" data-v-a6f0e41e><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-a6f0e41e><span class="vpi-align-left menu-icon" data-v-a6f0e41e></span><span class="menu-text" data-v-a6f0e41e>菜单</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-a6f0e41e data-v-8a42e2b4><button data-v-8a42e2b4>回到顶部</button><!----></div></div></div><aside class="VPSidebar" data-v-5d98c3a5 data-v-319d5ca6><div class="curtain" data-v-319d5ca6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-319d5ca6><span class="visually-hidden" id="sidebar-aria-label" data-v-319d5ca6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="no-transition group" data-v-c40bc020><section class="VPSidebarItem level-0 has-active" data-v-c40bc020 data-v-b3fd67f8><!----><div class="items" data-v-b3fd67f8><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b3fd67f8 data-v-b3fd67f8><div class="item" data-v-b3fd67f8><div class="indicator" data-v-b3fd67f8></div><a class="VPLink link link" href="/%E7%88%AC%E8%99%AB/%E5%AF%BC%E8%AE%BA.html" data-v-b3fd67f8><!--[--><p class="text" data-v-b3fd67f8>导论</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b3fd67f8 data-v-b3fd67f8><div class="item" data-v-b3fd67f8><div class="indicator" data-v-b3fd67f8></div><a class="VPLink link link" href="/%E7%88%AC%E8%99%AB/%E6%8A%93%E5%8C%85.html" data-v-b3fd67f8><!--[--><p class="text" data-v-b3fd67f8>抓包</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b3fd67f8 data-v-b3fd67f8><div class="item" data-v-b3fd67f8><div class="indicator" data-v-b3fd67f8></div><a class="VPLink link link" href="/%E7%88%AC%E8%99%AB/bs4%E8%A7%A3%E6%9E%90.html" data-v-b3fd67f8><!--[--><p class="text" data-v-b3fd67f8>bs4解析</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b3fd67f8 data-v-b3fd67f8><div class="item" data-v-b3fd67f8><div class="indicator" data-v-b3fd67f8></div><a class="VPLink link link" href="/%E7%88%AC%E8%99%AB/xpath%E8%A7%A3%E6%9E%90.html" data-v-b3fd67f8><!--[--><p class="text" data-v-b3fd67f8>xpath解析</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b3fd67f8 data-v-b3fd67f8><div class="item" data-v-b3fd67f8><div class="indicator" data-v-b3fd67f8></div><a class="VPLink link link" href="/%E7%88%AC%E8%99%AB/%E7%99%BB%E5%BD%95%E6%B5%81%E7%A8%8B.html" data-v-b3fd67f8><!--[--><p class="text" data-v-b3fd67f8>登录流程</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b3fd67f8 data-v-b3fd67f8><div class="item" data-v-b3fd67f8><div class="indicator" data-v-b3fd67f8></div><a class="VPLink link link" href="/%E7%88%AC%E8%99%AB/scrapy.html" data-v-b3fd67f8><!--[--><p class="text" data-v-b3fd67f8>scrapy</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b3fd67f8 data-v-b3fd67f8><div class="item" data-v-b3fd67f8><div class="indicator" data-v-b3fd67f8></div><a class="VPLink link link" href="/%E7%88%AC%E8%99%AB/selenium.html" data-v-b3fd67f8><!--[--><p class="text" data-v-b3fd67f8>selenium</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b3fd67f8 data-v-b3fd67f8><div class="item" data-v-b3fd67f8><div class="indicator" data-v-b3fd67f8></div><a class="VPLink link link" href="/%E7%88%AC%E8%99%AB/jsonpath%E8%A7%A3%E6%9E%90.html" data-v-b3fd67f8><!--[--><p class="text" data-v-b3fd67f8>jsonpath解析</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-5d98c3a5 data-v-1428d186><div class="VPDoc has-sidebar has-aside" data-v-1428d186 data-v-39a288b8><!--[--><!--]--><div class="container" data-v-39a288b8><div class="aside" data-v-39a288b8><div class="aside-curtain" data-v-39a288b8></div><div class="aside-container" data-v-39a288b8><div class="aside-content" data-v-39a288b8><div class="VPDocAside" data-v-39a288b8 data-v-3f215769><!--[--><!--]--><!--[--><!--]--><nav aria-labelledby="doc-outline-aria-label" class="VPDocAsideOutline" data-v-3f215769 data-v-a5bbad30><div class="content" data-v-a5bbad30><div class="outline-marker" data-v-a5bbad30></div><div aria-level="2" class="outline-title" id="doc-outline-aria-label" role="heading" data-v-a5bbad30>On this page</div><ul class="VPDocOutlineItem root" data-v-a5bbad30 data-v-b933a997><!--[--><!--]--></ul></div></nav><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-39a288b8><div class="content-container" data-v-39a288b8><!--[--><!--]--><main class="main" data-v-39a288b8><div style="position:relative;" class="vp-doc _%E7%88%AC%E8%99%AB_selenium" data-v-39a288b8><div><h1 id="selenium基础" tabindex="-1">selenium基础 <a class="header-anchor" href="#selenium基础" aria-label="Permalink to &quot;selenium基础&quot;">​</a></h1><h2 id="简介" tabindex="-1">简介 <a class="header-anchor" href="#简介" aria-label="Permalink to &quot;简介&quot;">​</a></h2><ul><li>selenium <ul><li><p>是一种浏览器自动化的工具，所谓的自动化是指，我们可以通过代码的形式制定一系列的行为动作，然后执行代码，这些动作就会同步触发在浏览器中</p></li><li><p>便捷获取网站中动态加载的数据，模拟登录</p></li><li><p>基于浏览器自动化的一个模块</p></li><li><p><em>好处：</em></p><p><em>可以节约时间节约脑力，不需要花时间去找到真正的网址</em></p><ul><li>坏处：效率低下</li></ul></li></ul></li></ul><h2 id="下载驱动" tabindex="-1">下载驱动 <a class="header-anchor" href="#下载驱动" aria-label="Permalink to &quot;下载驱动&quot;">​</a></h2><blockquote><p>驱动路径：<a href="https://registry.npmmirror.com/binary.html?path=chromedriver/" target="_blank" rel="noreferrer">https://registry.npmmirror.com/binary.html?path=chromedriver/</a></p><p>版本号114之后驱动：<a href="https://googlechromelabs.github.io/chrome-for-testing/#stable" target="_blank" rel="noreferrer">Chrome for Testing availability</a></p><p>将驱动复制到谷歌安装路径</p><p><img src="https://typora567.oss-cn-chengdu.aliyuncs.com/temp_picture/image-20230220154125510.png" alt="image-20230220154125510"></p></blockquote><ul><li><p>chrome://version/查看谷歌版本等相关信息</p></li><li><p>注意以下信息</p></li><li><p><img src="https://typora567.oss-cn-chengdu.aliyuncs.com/temp_picture/image-20230220153551322.png" alt="image-20230220153551322"></p></li><li><p>谷歌浏览器添加至环境变量</p></li></ul><h2 id="python操作selenium" tabindex="-1">python操作selenium <a class="header-anchor" href="#python操作selenium" aria-label="Permalink to &quot;python操作selenium&quot;">​</a></h2><ol><li><p><code>pip install selenium==4.2.0</code>—&gt; selenium中的webdriver存在被识别的指纹</p></li><li><p>用undetected_chromedriver代替selenium解决浏览器打不开网页<code>pip install undetected_chromedriver</code>–-&gt;参考<a href="https://blog.csdn.net/Scott0902/article/details/127024380" target="_blank" rel="noreferrer">(24条消息) 用undetected_chromedriver代替selenium解决浏览器打不开网页_Scott0902的博客-CSDN博客</a></p></li><li><p><em>文档：</em></p><p><em><a href="https://selenium-python-zh.readthedocs.io/en/latest/" target="_blank" rel="noreferrer">https://selenium-python-zh.readthedocs.io/en/latest/</a></em></p></li><li><p><a href="https://blog.csdn.net/qq_43125235/article/details/125601564" target="_blank" rel="noreferrer">基于Python的Selenium详细教程_selenium python_Smile丶Life丶的博客-CSDN博客</a></p></li></ol><h3 id="get-start" tabindex="-1">get_start <a class="header-anchor" href="#get-start" aria-label="Permalink to &quot;get_start&quot;">​</a></h3><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> time</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Chrome()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.get(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;https://www.baidu.com/&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.maximize_window()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">time.sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.quit()</span></span></code></pre></div><h3 id="driver的配置文件" tabindex="-1">driver的配置文件 <a class="header-anchor" href="#driver的配置文件" aria-label="Permalink to &quot;driver的配置文件&quot;">​</a></h3><div class="language-py vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">py</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> undetected_chromedriver </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">as</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#加载驱动</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">headers </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">    &#39;User-Agent&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.50&#39;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">url </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;http://http.tiqu.letecs.com/getip3?num=1&amp;type=1&amp;pro=&amp;city=0&amp;yys=0&amp;port=1&amp;pack=276876&amp;ts=0&amp;ys=0&amp;cs=0&amp;lb=1&amp;sb=0&amp;pb=4&amp;mr=1&amp;regions=&amp;gm=4&quot;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ip </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> requests.get(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">url</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">url,</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">headers</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">headers).text</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ip </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ip.strip(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\r\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ip</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 加载谷歌配置项目</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">option </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.ChromeOptions()</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 配置项1：代理</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">option.add_argument(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">f</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;--proxy-server=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">{</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ip</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">}</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 配置项2：缓存目录(手动打开的浏览器需要关闭)此项配置利于保存缓存信息如登录请求等</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">option.add_argument(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">r</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;">--user-data-dir=C:</span><span style="--shiki-light:#22863A;--shiki-light-font-weight:bold;--shiki-dark:#85E89D;--shiki-dark-font-weight:bold;">\U</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;">sers</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\S</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;">OIDE</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\A</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;">ppData</span><span style="--shiki-light:#22863A;--shiki-light-font-weight:bold;--shiki-dark:#85E89D;--shiki-dark-font-weight:bold;">\L</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;">ocal</span><span style="--shiki-light:#22863A;--shiki-light-font-weight:bold;--shiki-dark:#85E89D;--shiki-dark-font-weight:bold;">\G</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;">oogle</span><span style="--shiki-light:#22863A;--shiki-light-font-weight:bold;--shiki-dark:#85E89D;--shiki-dark-font-weight:bold;">\C</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;">hrome</span><span style="--shiki-light:#22863A;--shiki-light-font-weight:bold;--shiki-dark:#85E89D;--shiki-dark-font-weight:bold;">\U</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;">ser Data</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#不需要default,记得取消转义，</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># option.add_argument(&#39;--profile-directory=default&#39;)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Chrome(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">options</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">option)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.get(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;https://www.douban.com/&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></div><h3 id="浏览器创建" tabindex="-1"><strong>浏览器创建</strong> <a class="header-anchor" href="#浏览器创建" aria-label="Permalink to &quot;**浏览器创建**&quot;">​</a></h3><ul><li><p>Selenium支持非常多的浏览器，如Chrome、Firefox、Edge等.另外，也支持无界面浏览器。</p></li><li><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">  </span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Chrome()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Firefox()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Edge()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.PhantomJS()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Safari()</span></span></code></pre></div></li></ul><h3 id="浏览器操作" tabindex="-1">浏览器操作 <a class="header-anchor" href="#浏览器操作" aria-label="Permalink to &quot;浏览器操作&quot;">​</a></h3><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.maximize_window()</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 调用maximize_window方法，使浏览器窗口最大化显示</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.set_window_position(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1100</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">20</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置浏览器的窗口位置</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.set_window_size(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">900</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">900</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置浏览器的窗口大小</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.refresh()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.back()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.forward()</span></span></code></pre></div><p>相关信息</p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.title </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 页面标题</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.current_url</span></span></code></pre></div><p>截图</p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.get_screenshot_as_file(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;test.png&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></div><h3 id="元素定位" tabindex="-1"><strong>元素定位</strong> <a class="header-anchor" href="#元素定位" aria-label="Permalink to &quot;**元素定位**&quot;">​</a></h3><ul><li>webdriver 提供了一系列的元素定位方法，常用的有以下几种：</li><li>from selenium.webdriver.common.by import By</li><li>元素定位方法包含了2个系列： <ul><li><code>find_element()</code>系列：用于定位单个的页面元素。</li><li><code>find_elements()</code>系列：用于定位一组页面元素，获取到的是一组列表。</li></ul></li></ul><h4 id="_1、通过标签id属性定位" tabindex="-1">1、通过标签<code>id</code>属性定位 <a class="header-anchor" href="#_1、通过标签id属性定位" aria-label="Permalink to &quot;1、通过标签`id`属性定位&quot;">​</a></h4><ul><li><p><code>find_element(By.ID,&#39;XX&#39;)</code>id定位，根据元素的id属性值定位，最为方便且唯一，但有可能不存在，也可能动态生成。</p></li><li><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> time</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 导入selenium包</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver.common.by </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> By</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 打开指定（Firefox）浏览器</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Firefox()</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 指定加载页面</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.get(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;http://www.csdn.net&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 通过id属性获取搜索输入框</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">input_text </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> browser.find_element(By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ID</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;toolbar-search-input&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 向搜索输入框内输入selenium</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">input_text.send_keys(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;selenium&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置停留五秒后执行下一步</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">time.sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 关闭浏览器</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.quit()</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">### 2、通过标签`name`属性定位</span></span></code></pre></div><ul><li><code>find_element(By.NAME,&#39;xx&#39;)</code>name定位，根据元素的name属性值定位，定位到的标签不一定是唯一的。</li></ul></li></ul><h4 id="_2、通过标签name属性定位" tabindex="-1">2、通过标签<code>name</code>属性定位 <a class="header-anchor" href="#_2、通过标签name属性定位" aria-label="Permalink to &quot;2、通过标签`name`属性定位&quot;">​</a></h4><ul><li><p><code>find_element(By.NAME,&#39;xx&#39;)</code>name定位，根据元素的name属性值定位，定位到的标签不一定是唯一的。</p></li><li><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> time</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 导入selenium包</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver.common.by </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> By</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启动并打开指定页面</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Firefox()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.get(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;http://www.baidu.com/&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 通过name属性选择文本框元素，并设置内容</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.find_element(By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">NAME</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;wd&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).send_keys(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;selenium&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 通过通过ID属性获取“百度一下”按钮，并执行点击操作</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.find_element(By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ID</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;su&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).click()</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 停留五秒后关闭浏览器</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">time.sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.quit()</span></span></code></pre></div></li></ul><h4 id="_3、通过标签class属性定位" tabindex="-1">3、通过标签<code>class</code>属性定位 <a class="header-anchor" href="#_3、通过标签class属性定位" aria-label="Permalink to &quot;3、通过标签`class`属性定位&quot;">​</a></h4><ul><li><code>find_element_by(By.CLASS_NAME,&#39;xx&#39;)</code>class定位，根据元素的class属性值定位，但可能受JS影响动态变化。定位到的标签不一定是唯一的。</li></ul><h4 id="_4、通过标签tag定位" tabindex="-1">4、通过标签<code>tag</code>定位 <a class="header-anchor" href="#_4、通过标签tag定位" aria-label="Permalink to &quot;4、通过标签`tag`定位&quot;">​</a></h4><ul><li><code>find_element(By.TAG_NAME,&#39;xx&#39;)</code>tag name定位，根据元素的标签名定位，定位到的标签不一定是唯一的。</li></ul><h4 id="_5、通过link定位" tabindex="-1">5、通过link定位 <a class="header-anchor" href="#_5、通过link定位" aria-label="Permalink to &quot;5、通过link定位&quot;">​</a></h4><ul><li><p>link表示包含有属性href的标签元素，如:<a href="https://www.csdn.net">linktext</a>可以通过LINK＿TEXT进行定位。 <code>find_element(By.LINK_TEXT,&#39;XX&#39;)</code>根据链接文本全匹配进行精确定位。 <code>find_element(By.PARTIAL_LINK_TEXT,&#39;XX&#39;)</code>根据链接文本模糊匹配进行定位。</p><ol><li><p><code>By.LINK_TEXT</code>精确定位</p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> time</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 导入selenium包</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver.common.by </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> By</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启动并打开指定页面</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Firefox()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.get(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;http://www.csdn.net&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 选择&lt;a href=&quot;https://blog.csdn.net/nav/back-end&quot;&gt;Python&lt;/a&gt;标签,执行点击操作</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.find_element(By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">LINK_TEXT</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Python&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).click()</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 停留三秒后关闭浏览器</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">time.sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.quit()</span></span></code></pre></div></li><li><p><code>By.PARTIAL_LINK_TEXT</code>模糊定位</p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> time</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 导入selenium包</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver.common.by </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> By</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启动并打开指定页面</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Firefox()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.get(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;http://www.csdn.net&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 选择&lt;a href=&quot;href=&quot;https://blog.csdn.net/nav/ai&quot;&gt;人工智能&lt;/a&gt;标签,执行点击操作</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.find_element(By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">PARTIAL_LINK_TEXT</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;人工&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).click()</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 停留五秒后关闭浏览器</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">time.sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">browser.quit()</span></span></code></pre></div></li></ol></li></ul><h4 id="_6、通过xpath定位" tabindex="-1">6、通过XPATH定位 <a class="header-anchor" href="#_6、通过xpath定位" aria-label="Permalink to &quot;6、通过XPATH定位&quot;">​</a></h4><p><code>find_element(By.XPATH,&#39;xpath路径&#39;)</code></p><h3 id="节点交互" tabindex="-1">节点交互 <a class="header-anchor" href="#节点交互" aria-label="Permalink to &quot;节点交互&quot;">​</a></h3><ul><li>Selenium可以驱动浏览器来执行一些操作，也就是说可以让浏览器模拟执行一些动作。比较常见的用法有：输入文字时用<code>send_keys()</code>方法，清空文字时用<code>clear()</code>方法，点击按钮时用<code>click()</code>方法。</li></ul><h3 id="元素的基本属性和方法" tabindex="-1">元素的基本属性和方法 <a class="header-anchor" href="#元素的基本属性和方法" aria-label="Permalink to &quot;元素的基本属性和方法&quot;">​</a></h3><p><code>元素.属性</code></p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(el.size)</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(el.text)</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(el.get_attribute(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;class&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">))</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(el.is_displayed())  </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 是否可见</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(el.is_enabled())  </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 是否可用</span></span></code></pre></div><h3 id="执行js" tabindex="-1">执行js <a class="header-anchor" href="#执行js" aria-label="Permalink to &quot;执行js&quot;">​</a></h3><p>对于某些操作，Selenium API并没有提供。比如，下拉进度条，它可以直接模拟运行JavaScript，此时使用<code>execute_script()</code>方法即可实现。</p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.execute_script(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;window.scrollTo(0,2000)&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></div><h3 id="获取页面源码数据" tabindex="-1">获取页面源码数据 <a class="header-anchor" href="#获取页面源码数据" aria-label="Permalink to &quot;获取页面源码数据&quot;">​</a></h3><p>通过<code>page_source</code>属性可以获取网页的源代码，接着就可以使用解析库（如正则表达式、Beautiful Soup、pyquery等）来提取信息了。</p><p>思考：在爬虫中为什么需要使用selenium？selenium和爬虫之间的关联是什么？</p><ul><li><p>便捷的爬取动态加载数据（可见即可得）</p><ul><li><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#获取前5页的企业名称</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> time</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> lxml </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> etree</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Chrome(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">executable_path</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;./chromedriver&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">url </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &#39;http://scxk.nmpa.gov.cn:81/xk/&#39;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro.get(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">url</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">url)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">time.sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#获取页面源码数据(page_source)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">page_text </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> bro.page_source</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#将前5页的页面源码数据存储到该列表中</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">all_page_text_list </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [page_text]</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">for</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">in</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> range</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">4</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">):</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">    #点击下一页</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    next_page_btn </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> bro.find_element_by_xpath(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;//*[@id=&quot;pageIto_next&quot;]&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    next_page_btn.click()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    time.sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    all_page_text_list.append(bro.page_source)</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">for</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> page_text </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">in</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> all_page_text_list:</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">    #解析数据</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    tree </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> etree.HTML(page_text)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    li_list </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> tree.xpath(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;//*[@id=&quot;gzlist&quot;]/li&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">    for</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> li </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">in</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> li_list:</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">        title </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> li.xpath(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;./dl/@title&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">        print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(title)</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">time.sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro.quit()</span></span></code></pre></div></li></ul></li></ul><h3 id="等待" tabindex="-1">等待 <a class="header-anchor" href="#等待" aria-label="Permalink to &quot;等待&quot;">​</a></h3><ol><li><p>强制等待</p><p>time.sleep(2)</p></li><li><p>显式等待</p><blockquote><p>显式等待允许你等待某个条件成立，而不是盲目地等待一个固定的时间。</p></blockquote></li></ol><ul><li><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver.support.ui </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> WebDriverWait</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver.support </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> expected_conditions </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">as</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> EC</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置显式等待，最多等待3秒</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">wait </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> WebDriverWait(driver, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">try</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">    # 等待直到某个元素出现</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    element </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> wait.until(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">EC</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.presence_of_element_located((By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">XPATH</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;//*[@id=&#39;kw&#39;]&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)))</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">except</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">    print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;未找到元素&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    driver.close()</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">    exit</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">()</span></span></code></pre></div></li></ul><ol start="3"><li><p>隐式等待：隐式等待设置了一个全局等待时间，在这个时间内，WebDriver会等待某个元素出现。如果在设置的时间内找到了元素，WebDriver会继续执行；如果超时，则抛出<code>NoSuchElementException</code>异常。</p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.implicitly_wait(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></div></li></ol><h3 id="键盘鼠标操作" tabindex="-1">键盘鼠标操作 <a class="header-anchor" href="#键盘鼠标操作" aria-label="Permalink to &quot;键盘鼠标操作&quot;">​</a></h3><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver.common.keys </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Keys</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 发送普通按键</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">element.send_keys(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;文本&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 发送特殊按键</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">element.send_keys(Keys.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ENTER</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)  </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 回车</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">element.send_keys(Keys.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">TAB</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)    </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Tab键</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">element.send_keys(Keys.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ESCAPE</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># ESC键</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">el.send_keys(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;python&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,Keys.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ENTER</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></div><p>ActionChains复杂操作</p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver.common.action_chains </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ActionChains</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">actions </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ActionChains(driver)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">actions.click(el).send_keys(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;python&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).context_click(el)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">actions.perform() </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 所有事件链必须进行提交事务：perform</span></span></code></pre></div><p>double_click()</p><p>drap_and_drop() # 拖拽</p><p>move_to_element() # 悬停</p><h3 id="下拉选择框中的元素定位" tabindex="-1">下拉选择框中的元素定位 <a class="header-anchor" href="#下拉选择框中的元素定位" aria-label="Permalink to &quot;下拉选择框中的元素定位&quot;">​</a></h3><ol><li><p>创建Select对象select</p></li><li><p>通过select使用不同的方式去定位下拉框具体元素</p><ul><li>通过索引</li><li>通过文本内容</li><li>通过属性value</li></ul><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">select </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Select(el)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">select.select_by_index(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">select.select_by_visible_text(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;视频前贴片&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">select.select_by_value(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;bj&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">time.sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></div></li></ol><h3 id="警告框" tabindex="-1">警告框 <a class="header-anchor" href="#警告框" aria-label="Permalink to &quot;警告框&quot;">​</a></h3><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">alert1 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> driver.switch_to.alert </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 切入警告框</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">alert1.dismiss()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">alert1.accept()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">alert1.send_keys(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;t&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></div><h3 id="页面切换" tabindex="-1">页面切换 <a class="header-anchor" href="#页面切换" aria-label="Permalink to &quot;页面切换&quot;">​</a></h3><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(driver.window_handles)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.find_element(By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">XPATH</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).click()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.switch_to.window(driver.window_handles[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">])</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.find_element(By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">XPATH</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).click()</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(driver.window_handles)</span></span></code></pre></div><h3 id="设置cookie" tabindex="-1">设置cookie <a class="header-anchor" href="#设置cookie" aria-label="Permalink to &quot;设置cookie&quot;">​</a></h3><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.add_cookie({</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;name&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;value&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">})</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">driver.refresh() </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 必须</span></span></code></pre></div><h2 id="iframe处理以及动作链的使用" tabindex="-1">iframe处理以及动作链的使用 <a class="header-anchor" href="#iframe处理以及动作链的使用" aria-label="Permalink to &quot;iframe处理以及动作链的使用&quot;">​</a></h2><ul><li><p>iframe表示的子页面中，包含子html</p></li><li><p><img src="https://typora567.oss-cn-chengdu.aliyuncs.com/temp_picture/image-20230912210653972.png" alt="image-20230912210653972"></p></li><li><p>一些交互动作是针对某个节点执行的。比如，对于输入框，我们就调用它的输入文字和清空文字方法；对于按钮，就调用它的点击方法。其实，还有另外一些操作，它们没有特定的执行对象，比如鼠标拖曳、键盘按键等，这些动作用另一种方式来执行，那就是动作链。动作链【拖动】使用场景：网页用户登录时的滑块验证（也是一种验证方式）</p></li><li><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">selenium处理iframe　</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">　　-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> 如果定位的标签存在于iframe标签之中，则必须使用switch_to.frame(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">id</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">　　-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> 动作链（拖动）：</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ActionChains</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">　　-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> 实例化一个动作链对象：action </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ActionChains(bro)</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">　　-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> click_and_hold（div）：长按且点击操作</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">　　-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> move_by_offset(x,y)</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">　　-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> perform()让动作链立即执行</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">　　-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> action.release()释放动作链对象</span></span></code></pre></div></li></ul><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ActionChains</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> time </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> sleep</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Chrome(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">executable_path</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;./chromedriver&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro.get(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#注意：如果定位的标签是存在于iframe表示的子页面中，则常规的标签定位报错</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#处理：使用如下指定操作</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro.switch_to.frame(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;iframeResult&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">div_tag </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> bro.find_element_by_id(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;draggable&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#实例化一个动作链对象且将该对象绑定到指定的浏览器中</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">action </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ActionChains(bro)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">action.click_and_hold(div_tag) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#对指定标签实现点击且长按操作</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">for</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">in</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> range</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">):</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    action.move_by_offset(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).perform() </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#perform让动作链立即执行</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">    sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0.5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">action.release()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro.quit()</span></span></code></pre></div><h3 id="qq空间模拟登录" tabindex="-1">qq空间模拟登录 <a class="header-anchor" href="#qq空间模拟登录" aria-label="Permalink to &quot;qq空间模拟登录&quot;">​</a></h3><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver.common.by </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> By</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> time </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> sleep</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Chrome()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro.get(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;https://qzone.qq.com/&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#注意：如果定位的标签是存在于iframe表示的子页面中，则常规的标签定位报错</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#处理：使用如下指定操作</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro.switch_to.frame(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;login_frame&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">mima_login </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> bro.find_element(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">by</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ID</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">value</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;switcher_plogin&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">mima_login.click()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">user </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> bro.find_element(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">by</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ID</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">value</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;u&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">pwd </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> bro.find_element(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">by</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ID</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">value</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;p&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">user.send_keys(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;xxx&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">pwd.send_keys(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;xxx&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">button </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> bro.find_element(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">by</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">By.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ID</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">value</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;login_button&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">button.click()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro.quit()</span></span></code></pre></div><h2 id="google无头浏览器-规避检测selenium" tabindex="-1">Google无头浏览器+规避检测selenium <a class="header-anchor" href="#google无头浏览器-规避检测selenium" aria-label="Permalink to &quot;Google无头浏览器+规避检测selenium&quot;">​</a></h2><ul><li>无头浏览器：</li></ul><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver.chrome.options </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Options</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">option </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Options()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">option.add_argument(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;--headless&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">option.add_argument(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;--disable-gpu&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">bro </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> webdriver.Chrome(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;./chromedriver.exe&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">options</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">option)</span></span></code></pre></div><ul><li><p>规避检测：</p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> selenium.webdriver </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ChromeOptions</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">chrome_options </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ChromeOptions()</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">chrome_options.add_experimental_option(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;excludeSwitches&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;enable-automation&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">])</span></span></code></pre></div></li></ul></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-e257564d><!--[--><!--]--><!----><nav class="prev-next" aria-labelledby="doc-footer-aria-label" data-v-e257564d><span class="visually-hidden" id="doc-footer-aria-label" data-v-e257564d>Pager</span><div class="pager" data-v-e257564d><a class="VPLink link pager-link prev" href="/%E7%88%AC%E8%99%AB/scrapy.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>上一篇</span><span class="title" data-v-e257564d>scrapy</span><!--]--></a></div><div class="pager" data-v-e257564d><a class="VPLink link pager-link next" href="/%E7%88%AC%E8%99%AB/jsonpath%E8%A7%A3%E6%9E%90.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>下一篇</span><span class="title" data-v-e257564d>jsonpath解析</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><footer class="VPFooter has-sidebar" data-v-5d98c3a5 data-v-e315a0ad><div class="container" data-v-e315a0ad><p class="message" data-v-e315a0ad>Released under the MIT License.</p><p class="copyright" data-v-e315a0ad>Copyright © 2024.8.15-present 九问.All Rights Reserved.</p></div></footer><!--[--><!--]--></div><!----><!----><!--]--></div>
    <script>window.__VP_HASH_MAP__=JSON.parse("{\"index.md\":\"DPLzomJ3\",\"linux_linux基础入门.md\":\"pHBVwgKW\",\"linux_常用命令备忘.md\":\"BjZbNsQB\",\"linux_虚拟机相关.md\":\"509DUbJA\",\"llm_langchain_lcel表达式与runnable可运行协议.md\":\"Do8svL8L\",\"llm_langchain_model.md\":\"BseancJn\",\"llm_langchain_outputparser.md\":\"Dy6NsY5B\",\"llm_langchain_prompt.md\":\"XqZSAhnf\",\"llm_langchain_quick_start.md\":\"BNcILzfr\",\"python_原理类_依赖注入.md\":\"CL2SMX95\",\"python_原理类_内存管理.md\":\"CPaxdydH\",\"python_原理类_单例模式.md\":\"sMUaVqEG\",\"python_原理类_多进程多线程协程.md\":\"DqpQdOiz\",\"python_原理类_深浅拷贝.md\":\"CLyf8bhf\",\"python_原理类_自省.md\":\"xlfwK7Qt\",\"python_原理类_迭代器和生成器.md\":\"CkWqBBDs\",\"python_原理类_闭包和装饰器.md\":\"DK3E-cjV\",\"python_原理类_高阶函数.md\":\"DCh-efV4\",\"python_原理类_魔法方法.md\":\"DmwyLFne\",\"python_原理类_鸭子类型.md\":\"5JGfCqIC\",\"python_基础_练习题.md\":\"CsgELmqe\",\"python_工具类_logging.md\":\"dWLhofea\",\"python框架_django_4-1环境搭建.md\":\"B6koVIdo\",\"python框架_django_4-2settings配置.md\":\"B6f707F-\",\"python框架_django_4-3路由定义与使用.md\":\"BD3-Rmv3\",\"python框架_django_4-4请求与响应.md\":\"n_ivviS2\",\"python框架_django_4-5session_cookie.md\":\"DAeZMmLC\",\"python框架_django_4-6类视图.md\":\"CC_0FUMu\",\"python框架_django_4-7模板引擎.md\":\"BdpQJw0a\",\"python框架_django_接口文档.md\":\"cRd_13f9\",\"python框架_django_熟悉流程_部署博客.md\":\"B4LDDwJF\",\"python框架_django_虚拟环境.md\":\"CANoJLNU\",\"python框架_flask_flask-sqlalchemy.md\":\"CUU_aFQd\",\"python框架_flask_flask-wtf_wtfforms.md\":\"LvJO-ZqI\",\"python框架_flask_start.md\":\"CEtxJVgY\",\"其他_八股_linux.md\":\"CJveVc64\",\"其他_八股_其他_汇编_串指令.md\":\"BttYJMTr\",\"其他_八股_其他_汇编_前置知识.md\":\"BucxfzEY\",\"其他_八股_其他_汇编_宏汇编.md\":\"2UfLWMAK\",\"其他_八股_其他_汇编_寄存器.md\":\"jp1xBRVS\",\"其他_八股_其他_汇编_寻址方式.md\":\"CewV_a8e\",\"其他_八股_其他_汇编_数据传送指令.md\":\"PY1RmkQj\",\"其他_八股_其他_汇编_程序设计综合.md\":\"CqsM9zoN\",\"其他_八股_其他_汇编_逻辑运算与移位指令.md\":\"B_-kKKiB\",\"其他_八股_其他_网安_网络安全.md\":\"COiOJXPx\",\"其他_八股_前端_css_单位.md\":\"DyaFOJT4\",\"其他_八股_前端_css_居中的方式.md\":\"rsk20pg5\",\"其他_八股_前端_html_文档声明.md\":\"CQQLsheK\",\"其他_八股_前端_html_替换元素.md\":\"D69yt3g4\",\"其他_八股_前端_html_语义化_seo_微格式.md\":\"DcyemF8i\",\"其他_八股_前端_html_页面可见性.md\":\"CFWJeBWr\",\"其他_八股_前端_事件循环_异步.md\":\"pX3ui26Y\",\"其他_八股_操作系统_存储管理.md\":\"BiNbZ62S\",\"其他_八股_操作系统_操作系统安全及虚拟化技术概述.md\":\"JHACDhW4\",\"其他_八股_操作系统_操作系统概述及基本概念.md\":\"D9Lo515v\",\"其他_八股_操作系统_文件系统.md\":\"Dtjdw_je\",\"其他_八股_操作系统_死锁.md\":\"zHFIU7qo\",\"其他_八股_操作系统_输入输出.md\":\"frvtHq-m\",\"其他_八股_操作系统_进程与线程.md\":\"DbHOm3Wq\",\"其他_八股_操作系统_面试八股.md\":\"CzgRxFHm\",\"其他_八股_数据库_log.md\":\"B-ybS9bR\",\"其他_八股_数据库_mysql的执行引擎.md\":\"CUp82kj5\",\"其他_八股_数据库_redis.md\":\"z8vp5n46\",\"其他_八股_数据库_sql语句的执行.md\":\"B0aqkw_M\",\"其他_八股_数据库_事务_事务的四大特性.md\":\"DWegFK6Q\",\"其他_八股_数据库_事务_事务的隔离级别.md\":\"CJpAB-SH\",\"其他_八股_数据库_慢查询.md\":\"C1IK6Wix\",\"其他_八股_数据库_索引失效的场景.md\":\"Ax0k9vQn\",\"其他_八股_数据库_进阶_窗口函数.md\":\"C_zDFJPI\",\"其他_八股_数据结构_二分法.md\":\"cV9m7f2a\",\"其他_八股_数据结构_二叉树.md\":\"DkYu-9Db\",\"其他_八股_数据结构_回溯.md\":\"CcyoEaKv\",\"其他_八股_数据结构_数组_蛇形填数.md\":\"sJgKru0T\",\"其他_八股_数据结构_滑动窗口最大值.md\":\"7EhHWPLR\",\"其他_八股_数据结构_背包_01背包.md\":\"Bm-yOyBt\",\"其他_八股_数据结构_链表_两两交换链表中的节点.md\":\"B8MvoYxB\",\"其他_八股_数据结构_链表_单链表.md\":\"BrcntfvU\",\"其他_八股_数据结构_链表_反转链表.md\":\"imXNe7Cp\",\"其他_八股_测开_bug.md\":\"DEH0Pngz\",\"其他_八股_测开_性能测试.md\":\"BIq1PtDn\",\"其他_八股_测开_测试的流程.md\":\"BDpHuwv1\",\"其他_八股_测开_测试的理解.md\":\"D0oj3MOT\",\"其他_八股_测开_软件测试.md\":\"DBQvBkbe\",\"其他_八股_测开_软件质量.md\":\"BbbVdNmM\",\"其他_八股_计网_cdn.md\":\"BBkFV3YX\",\"其他_八股_计网_cookie_session.md\":\"9lTHujdv\",\"其他_八股_计网_dns查询过程.md\":\"UEG8MFOS\",\"其他_八股_计网_http常见状态码.md\":\"FjHGrHsS\",\"其他_八股_计网_osi模型.md\":\"CQQrgYYo\",\"其他_八股_计网_url到页面展示.md\":\"CysFBpDy\",\"其他_八股_计网_传输层_tcp_udp.md\":\"CGN96CrC\",\"其他_八股_计网_应用层_http.md\":\"BwfPa-_I\",\"其他_八股_计网_应用层_http1.0各个版本.md\":\"Po_QFcD7\",\"其他_八股_计网_应用层_http_https.md\":\"CVNCJSvP\",\"其他_八股_计网_强缓存和协商缓存.md\":\"BCS9u_As\",\"其他_八股_计网_物理层_奈氏准则_香农公式.md\":\"LJ2m6SOu\",\"其他_面经_大疆.md\":\"7GCAKeVb\",\"其他_面经_差旅1号.md\":\"COJ0oCTe\",\"其他_面经_广州嘉为科技公司-python.md\":\"DKqZln1v\",\"其他_面经_得物.md\":\"Dh7JuSj9\",\"其他_面经_招商银行.md\":\"xRFKUhWx\",\"其他_面经_百度.md\":\"BNUT_qAS\",\"其他_项目_项目部署_vitepress_githubpages部署.md\":\"BVpDdxkG\",\"其他_项目_项目部署_vitepress_netlify部署.md\":\"Coqp4VTV\",\"其他_项目_项目部署_vitepress_nginx部署.md\":\"BAeOv9Kq\",\"其他_项目_项目部署_web.md\":\"BoGi1t8O\",\"前端_vue3_axios.md\":\"D33GJUpk\",\"前端_vue3_面试.md\":\"Ba2Ot3Rt\",\"前端_前端三件套_css_css3自定义属性.md\":\"D8gxja-i\",\"前端_前端三件套_css_css基础入门.md\":\"ndtMtB1o\",\"前端_前端三件套_css_flexbox布局.md\":\"THfIflcE\",\"前端_前端三件套_css_grid布局.md\":\"DxVJntk3\",\"前端_前端三件套_css_动画.md\":\"CFLVIA4-\",\"前端_前端三件套_css_媒体查询.md\":\"DOMiDDiY\",\"前端_前端三件套_css_属性.md\":\"CESPzocp\",\"前端_前端三件套_html_html基础.md\":\"DVzBORCM\",\"前端_前端三件套_js_dom操作.md\":\"DMHjLETt\",\"前端_前端三件套_js_js入门.md\":\"B5ZmIMru\",\"前端_前端三件套_js_属性描述符.md\":\"CbnoBHiZ\",\"前端_前端三件套_js_高阶函数.md\":\"xnE4baNa\",\"前端_前端三件套_实战案例_聊天页面.md\":\"Biaa-glg\",\"前端_前端三件套_面试_基本题.md\":\"CHZnvxX-\",\"工具_git_gitignore.md\":\"DoXDP0e-\",\"工具_git_git的基本使用.md\":\"DU6wlewG\",\"工具_git_同时推送gitee和github.md\":\"Cvg2MFU1\",\"测试_faker数据随机生成.md\":\"BasZvW7O\",\"测试_jenkins.md\":\"CFQxIQZz\",\"测试_pytest_pytest基础.md\":\"6gYMEMqz\",\"测试_pytest_yaml文件.md\":\"kC0gde-v\",\"测试_pytest_日志.md\":\"CXf5niRD\",\"测试_unittest.md\":\"DLCFrt8V\",\"测试_性能测试_基础概念.md\":\"DOav1Wqa\",\"爬虫_bs4解析.md\":\"DuOzYyCe\",\"爬虫_jsonpath解析.md\":\"Drhtc37w\",\"爬虫_scrapy.md\":\"D83vZHmx\",\"爬虫_selenium.md\":\"BhDk9UyV\",\"爬虫_xpath解析.md\":\"MjtoQ3qX\",\"爬虫_导论.md\":\"BiquNqIP\",\"爬虫_抓包.md\":\"BDJtadra\",\"爬虫_登录流程.md\":\"MPghsp8c\",\"环境搭建_docker_mysql.md\":\"Duy1yYHv\"}");window.__VP_SITE_DATA__=JSON.parse("{\"lang\":\"en-US\",\"dir\":\"ltr\",\"title\":\"九问Blog\",\"description\":\"欢迎访问九问Blog！分享一些Python全栈开发以及爬虫技术，希望对你有所帮助\",\"base\":\"/\",\"head\":[],\"router\":{\"prefetchLinks\":true},\"appearance\":true,\"themeConfig\":{\"socialLinks\":[{\"icon\":\"github\",\"link\":\"https://github.com/jiuwen567\"},{\"icon\":{\"svg\":\"<svg t=\\\"1724307958358\\\" class=\\\"icon\\\" viewBox=\\\"0 0 1024 1024\\\" version=\\\"1.1\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" p-id=\\\"4234\\\" width=\\\"200\\\" height=\\\"200\\\"><path d=\\\"M512 0C230.4 0 0 230.4 0 512s230.4 512 512 512 512-230.4 512-512S793.6 0 512 0z m284.8 313.6c0 12.8-12.8 25.6-25.6 25.6H416c-41.6 0-76.8 35.2-76.8 76.8v243.2c0 12.8 12.8 25.6 25.6 25.6h240c41.6 0 76.8-35.2 76.8-76.8v-12.8c0-12.8-12.8-25.6-25.6-25.6H480c-12.8 0-25.6-12.8-25.6-25.6v-64c0-12.8 12.8-25.6 25.6-25.6h291.2c12.8 0 25.6 12.8 25.6 25.6v144c0 92.8-76.8 169.6-169.6 169.6H252.8c-12.8 0-25.6-12.8-25.6-25.6V412.8C227.2 310.4 310.4 224 416 224h355.2c12.8 0 25.6 12.8 25.6 25.6v64z\\\" fill=\\\"#B32225\\\" p-id=\\\"4235\\\"></path></svg>\"},\"link\":\"https://gitee.com/jiuwen567\"}],\"footer\":{\"message\":\"Released under the MIT License.\",\"copyright\":\"Copyright © 2024.8.15-present 九问.All Rights Reserved.\"},\"search\":{\"provider\":\"local\"},\"docFooter\":{\"prev\":\"上一篇\",\"next\":\"下一篇\"},\"returnToTopLabel\":\"回到顶部\",\"sidebarMenuLabel\":\"菜单\",\"darkModeSwitchLabel\":\"主题\",\"lightModeSwitchTitle\":\"切换到浅色模式\",\"darkModeSwitchTitle\":\"切换到深色模式\",\"nav\":[{\"text\":\"工具\",\"activeMatch\":\"/工具/\",\"link\":\"/工具/Git/同时推送Gitee和GitHub\"},{\"text\":\"Linux\",\"activeMatch\":\"/Linux/\",\"link\":\"/Linux/Linux基础入门\"},{\"text\":\"爬虫\",\"activeMatch\":\"/爬虫/\",\"link\":\"/爬虫/导论\"},{\"text\":\"测试\",\"activeMatch\":\"/测试/\",\"link\":\"/测试/faker数据随机生成\"},{\"text\":\"Python\",\"activeMatch\":\"/Python/\",\"link\":\"/Python/原理类/内存管理\"},{\"text\":\"Python框架\",\"activeMatch\":\"/Python框架/\",\"link\":\"/Python框架/Django/接口文档\"},{\"text\":\"其他\",\"activeMatch\":\"/其他/\",\"link\":\"/其他/八股/Linux\"},{\"text\":\"前端\",\"activeMatch\":\"/前端/\",\"link\":\"/前端/Vue3/axios\"},{\"text\":\"LLM\",\"activeMatch\":\"/LLM/\",\"link\":\"/LLM/LangChain/quick_start\"},{\"text\":\"环境搭建\",\"activeMatch\":\"/环境搭建/\",\"link\":\"/环境搭建/docker/mysql\"}],\"sidebar\":{\"/工具/\":[{\"text\":\"Git\",\"collapsed\":false,\"items\":[{\"text\":\"同时推送Gitee和GitHub\",\"link\":\"/工具/Git/同时推送Gitee和GitHub\"},{\"text\":\"Git的基本使用\",\"link\":\"/工具/Git/Git的基本使用\"},{\"text\":\"gitignore\",\"link\":\"/工具/Git/gitignore\"}]}],\"/Linux/\":[{\"text\":\"Linux基础入门\",\"link\":\"/Linux/Linux基础入门\"},{\"text\":\"虚拟机相关\",\"link\":\"/Linux/虚拟机相关\"},{\"text\":\"常用命令备忘\",\"link\":\"/Linux/常用命令备忘\"}],\"/爬虫/\":[{\"text\":\"导论\",\"link\":\"/爬虫/导论\"},{\"text\":\"抓包\",\"link\":\"/爬虫/抓包\"},{\"text\":\"bs4解析\",\"link\":\"/爬虫/bs4解析\"},{\"text\":\"xpath解析\",\"link\":\"/爬虫/xpath解析\"},{\"text\":\"登录流程\",\"link\":\"/爬虫/登录流程\"},{\"text\":\"scrapy\",\"link\":\"/爬虫/scrapy\"},{\"text\":\"selenium\",\"link\":\"/爬虫/selenium\"},{\"text\":\"jsonpath解析\",\"link\":\"/爬虫/jsonpath解析\"}],\"/测试/\":[{\"text\":\"faker数据随机生成\",\"link\":\"/测试/faker数据随机生成\"},{\"text\":\"Jenkins\",\"link\":\"/测试/Jenkins\"},{\"text\":\"unittest\",\"link\":\"/测试/unittest\"},{\"text\":\"性能测试\",\"collapsed\":false,\"items\":[{\"text\":\"基础概念\",\"link\":\"/测试/性能测试/基础概念\"}]},{\"text\":\"pytest\",\"collapsed\":false,\"items\":[{\"text\":\"pytest基础\",\"link\":\"/测试/pytest/pytest基础\"},{\"text\":\"yaml文件\",\"link\":\"/测试/pytest/yaml文件\"},{\"text\":\"日志\",\"link\":\"/测试/pytest/日志\"}]}],\"/Python/\":[{\"text\":\"原理类\",\"collapsed\":false,\"items\":[{\"text\":\"内存管理\",\"link\":\"/Python/原理类/内存管理\"},{\"text\":\"单例模式\",\"link\":\"/Python/原理类/单例模式\"},{\"text\":\"多进程多线程协程\",\"link\":\"/Python/原理类/多进程多线程协程\"},{\"text\":\"深浅拷贝\",\"link\":\"/Python/原理类/深浅拷贝\"},{\"text\":\"自省\",\"link\":\"/Python/原理类/自省\"},{\"text\":\"迭代器和生成器\",\"link\":\"/Python/原理类/迭代器和生成器\"},{\"text\":\"闭包和装饰器\",\"link\":\"/Python/原理类/闭包和装饰器\"},{\"text\":\"高阶函数\",\"link\":\"/Python/原理类/高阶函数\"},{\"text\":\"魔法方法\",\"link\":\"/Python/原理类/魔法方法\"},{\"text\":\"鸭子类型\",\"link\":\"/Python/原理类/鸭子类型\"},{\"text\":\"依赖注入\",\"link\":\"/Python/原理类/依赖注入\"}]},{\"text\":\"工具类\",\"collapsed\":false,\"items\":[{\"text\":\"logging\",\"link\":\"/Python/工具类/logging\"}]},{\"text\":\"基础\",\"collapsed\":false,\"items\":[{\"text\":\"练习题\",\"link\":\"/Python/基础/练习题\"}]}],\"/前端/\":[{\"text\":\"Vue3\",\"collapsed\":false,\"items\":[{\"text\":\"axios\",\"link\":\"/前端/Vue3/axios\"},{\"text\":\"面试\",\"link\":\"/前端/Vue3/面试\"}]},{\"text\":\"前端三件套\",\"collapsed\":false,\"items\":[{\"text\":\"HTML\",\"collapsed\":false,\"items\":[{\"text\":\"HTML基础\",\"link\":\"/前端/前端三件套/HTML/HTML基础\"}]},{\"text\":\"CSS\",\"collapsed\":false,\"items\":[{\"text\":\"css3自定义属性\",\"link\":\"/前端/前端三件套/CSS/css3自定义属性\"},{\"text\":\"css基础入门\",\"link\":\"/前端/前端三件套/CSS/css基础入门\"},{\"text\":\"flexbox布局\",\"link\":\"/前端/前端三件套/CSS/flexbox布局\"},{\"text\":\"grid布局\",\"link\":\"/前端/前端三件套/CSS/grid布局\"},{\"text\":\"动画\",\"link\":\"/前端/前端三件套/CSS/动画\"},{\"text\":\"媒体查询\",\"link\":\"/前端/前端三件套/CSS/媒体查询\"},{\"text\":\"属性\",\"link\":\"/前端/前端三件套/CSS/属性\"}]},{\"text\":\"js\",\"collapsed\":false,\"items\":[{\"text\":\"DOM操作\",\"link\":\"/前端/前端三件套/js/DOM操作\"},{\"text\":\"js入门\",\"link\":\"/前端/前端三件套/js/js入门\"},{\"text\":\"属性描述符\",\"link\":\"/前端/前端三件套/js/属性描述符\"},{\"text\":\"高阶函数\",\"link\":\"/前端/前端三件套/js/高阶函数\"}]},{\"text\":\"面试\",\"collapsed\":false,\"items\":[{\"text\":\"基本题\",\"link\":\"/前端/前端三件套/面试/基本题\"}]},{\"text\":\"实战案例\",\"collapsed\":false,\"items\":[{\"text\":\"聊天页面\",\"link\":\"/前端/前端三件套/实战案例/聊天页面\"}]}]}],\"/Python框架/\":[{\"text\":\"Django\",\"collapsed\":false,\"items\":[{\"text\":\"接口文档\",\"link\":\"/Python框架/Django/接口文档\"},{\"text\":\"熟悉流程\",\"collapsed\":false,\"items\":[{\"text\":\"部署博客\",\"link\":\"/Python框架/Django/熟悉流程/部署博客\"}]},{\"text\":\"4-1环境搭建\",\"link\":\"/Python框架/Django/4-1环境搭建\"},{\"text\":\"4-2settings配置\",\"link\":\"/Python框架/Django/4-2settings配置\"},{\"text\":\"4-3路由定义与使用\",\"link\":\"/Python框架/Django/4-3路由定义与使用\"},{\"text\":\"4-4请求与响应\",\"link\":\"/Python框架/Django/4-4请求与响应\"},{\"text\":\"4-5session_cookie\",\"link\":\"/Python框架/Django/4-5session_cookie\"},{\"text\":\"4-6类视图\",\"link\":\"/Python框架/Django/4-6类视图\"},{\"text\":\"4-7模板引擎\",\"link\":\"/Python框架/Django/4-7模板引擎\"},{\"text\":\"虚拟环境\",\"link\":\"/Python框架/Django/虚拟环境\"}]},{\"text\":\"Flask\",\"collapsed\":false,\"items\":[{\"text\":\"start\",\"link\":\"/Python框架/Flask/start\"},{\"text\":\"Flask-wtf&wtfforms\",\"link\":\"/Python框架/Flask/Flask-wtf&wtfforms\"},{\"text\":\"Flask-SQLAlchemy\",\"link\":\"/Python框架/Flask/Flask-SQLAlchemy\"}]}],\"/其他/\":[{\"text\":\"面经\",\"collapsed\":false,\"items\":[{\"text\":\"大疆\",\"link\":\"/其他/面经/大疆\"},{\"text\":\"差旅1号\",\"link\":\"/其他/面经/差旅1号\"},{\"text\":\"广州嘉为科技公司-python\",\"link\":\"/其他/面经/广州嘉为科技公司-python\"},{\"text\":\"得物\",\"link\":\"/其他/面经/得物\"},{\"text\":\"招商银行\",\"link\":\"/其他/面经/招商银行\"},{\"text\":\"百度\",\"link\":\"/其他/面经/百度\"}]},{\"text\":\"八股\",\"collapsed\":false,\"items\":[{\"text\":\"Linux\",\"link\":\"/其他/八股/Linux\"},{\"text\":\"前端\",\"collapsed\":false,\"items\":[{\"text\":\"事件循环&异步\",\"link\":\"/其他/八股/前端/事件循环&异步\"},{\"text\":\"HTML\",\"collapsed\":false,\"items\":[{\"text\":\"文档声明\",\"link\":\"/其他/八股/前端/HTML/文档声明\"},{\"text\":\"替换元素\",\"link\":\"/其他/八股/前端/HTML/替换元素\"},{\"text\":\"语义化&SEO&微格式\",\"link\":\"/其他/八股/前端/HTML/语义化&SEO&微格式\"},{\"text\":\"页面可见性\",\"link\":\"/其他/八股/前端/HTML/页面可见性\"}]},{\"text\":\"CSS\",\"collapsed\":false,\"items\":[{\"text\":\"单位\",\"link\":\"/其他/八股/前端/CSS/单位\"},{\"text\":\"居中的方式\",\"link\":\"/其他/八股/前端/CSS/居中的方式\"}]}]},{\"text\":\"数据库\",\"collapsed\":false,\"items\":[{\"text\":\"log\",\"link\":\"/其他/八股/数据库/log\"},{\"text\":\"mysql的执行引擎\",\"link\":\"/其他/八股/数据库/mysql的执行引擎\"},{\"text\":\"redis\",\"link\":\"/其他/八股/数据库/redis\"},{\"text\":\"sql语句的执行\",\"link\":\"/其他/八股/数据库/sql语句的执行\"},{\"text\":\"慢查询\",\"link\":\"/其他/八股/数据库/慢查询\"},{\"text\":\"索引失效的场景\",\"link\":\"/其他/八股/数据库/索引失效的场景\"},{\"text\":\"事务\",\"collapsed\":false,\"items\":[{\"text\":\"事务的四大特性\",\"link\":\"/其他/八股/数据库/事务/事务的四大特性\"},{\"text\":\"事务的隔离级别\",\"link\":\"/其他/八股/数据库/事务/事务的隔离级别\"}]},{\"text\":\"进阶\",\"collapsed\":false,\"items\":[{\"text\":\"窗口函数\",\"link\":\"/其他/八股/数据库/进阶/窗口函数\"}]}]},{\"text\":\"测开\",\"collapsed\":false,\"items\":[{\"text\":\"bug\",\"link\":\"/其他/八股/测开/bug\"},{\"text\":\"性能测试\",\"link\":\"/其他/八股/测开/性能测试\"},{\"text\":\"测试的流程\",\"link\":\"/其他/八股/测开/测试的流程\"},{\"text\":\"测试的理解\",\"link\":\"/其他/八股/测开/测试的理解\"},{\"text\":\"软件测试\",\"link\":\"/其他/八股/测开/软件测试\"},{\"text\":\"软件质量\",\"link\":\"/其他/八股/测开/软件质量\"}]},{\"text\":\"数据结构\",\"collapsed\":false,\"items\":[{\"text\":\"二分法\",\"link\":\"/其他/八股/数据结构/二分法\"},{\"text\":\"二叉树\",\"link\":\"/其他/八股/数据结构/二叉树\"},{\"text\":\"回溯\",\"link\":\"/其他/八股/数据结构/回溯\"},{\"text\":\"滑动窗口最大值\",\"link\":\"/其他/八股/数据结构/滑动窗口最大值\"},{\"text\":\"数组\",\"collapsed\":false,\"items\":[{\"text\":\"蛇形填数\",\"link\":\"/其他/八股/数据结构/数组/蛇形填数\"}]},{\"text\":\"背包\",\"collapsed\":false,\"items\":[{\"text\":\"01背包\",\"link\":\"/其他/八股/数据结构/背包/01背包\"}]},{\"text\":\"链表\",\"collapsed\":false,\"items\":[{\"text\":\"两两交换链表中的节点\",\"link\":\"/其他/八股/数据结构/链表/两两交换链表中的节点\"},{\"text\":\"单链表\",\"link\":\"/其他/八股/数据结构/链表/单链表\"},{\"text\":\"反转链表\",\"link\":\"/其他/八股/数据结构/链表/反转链表\"}]}]},{\"text\":\"计网\",\"collapsed\":false,\"items\":[{\"text\":\"CDN\",\"link\":\"/其他/八股/计网/CDN\"},{\"text\":\"cookie&session\",\"link\":\"/其他/八股/计网/cookie&session\"},{\"text\":\"DNS查询过程\",\"link\":\"/其他/八股/计网/DNS查询过程\"},{\"text\":\"http常见状态码\",\"link\":\"/其他/八股/计网/http常见状态码\"},{\"text\":\"OSI模型\",\"link\":\"/其他/八股/计网/OSI模型\"},{\"text\":\"url到页面展示\",\"link\":\"/其他/八股/计网/url到页面展示\"},{\"text\":\"强缓存和协商缓存\",\"link\":\"/其他/八股/计网/强缓存和协商缓存\"},{\"text\":\"传输层\",\"collapsed\":false,\"items\":[{\"text\":\"TCP&UDP\",\"link\":\"/其他/八股/计网/传输层/TCP&UDP\"}]},{\"text\":\"物理层\",\"collapsed\":false,\"items\":[{\"text\":\"奈氏准则&香农公式\",\"link\":\"/其他/八股/计网/物理层/奈氏准则&香农公式\"}]},{\"text\":\"应用层\",\"collapsed\":false,\"items\":[{\"text\":\"http&https\",\"link\":\"/其他/八股/计网/应用层/http&https\"},{\"text\":\"http\",\"link\":\"/其他/八股/计网/应用层/http\"},{\"text\":\"http1.0各个版本\",\"link\":\"/其他/八股/计网/应用层/http1.0各个版本\"}]}]},{\"text\":\"操作系统\",\"collapsed\":false,\"items\":[{\"text\":\"存储管理\",\"link\":\"/其他/八股/操作系统/存储管理\"},{\"text\":\"操作系统安全及虚拟化技术概述\",\"link\":\"/其他/八股/操作系统/操作系统安全及虚拟化技术概述\"},{\"text\":\"操作系统概述及基本概念\",\"link\":\"/其他/八股/操作系统/操作系统概述及基本概念\"},{\"text\":\"文件系统\",\"link\":\"/其他/八股/操作系统/文件系统\"},{\"text\":\"死锁\",\"link\":\"/其他/八股/操作系统/死锁\"},{\"text\":\"输入输出\",\"link\":\"/其他/八股/操作系统/输入输出\"},{\"text\":\"进程与线程\",\"link\":\"/其他/八股/操作系统/进程与线程\"},{\"text\":\"面试八股\",\"link\":\"/其他/八股/操作系统/面试八股\"}]},{\"text\":\"其他\",\"collapsed\":false,\"items\":[{\"text\":\"汇编\",\"collapsed\":false,\"items\":[{\"text\":\"串指令\",\"link\":\"/其他/八股/其他/汇编/串指令\"},{\"text\":\"前置知识\",\"link\":\"/其他/八股/其他/汇编/前置知识\"},{\"text\":\"宏汇编\",\"link\":\"/其他/八股/其他/汇编/宏汇编\"},{\"text\":\"寄存器\",\"link\":\"/其他/八股/其他/汇编/寄存器\"},{\"text\":\"寻址方式\",\"link\":\"/其他/八股/其他/汇编/寻址方式\"},{\"text\":\"数据传送指令\",\"link\":\"/其他/八股/其他/汇编/数据传送指令\"},{\"text\":\"程序设计综合\",\"link\":\"/其他/八股/其他/汇编/程序设计综合\"},{\"text\":\"逻辑运算与移位指令\",\"link\":\"/其他/八股/其他/汇编/逻辑运算与移位指令\"}]},{\"text\":\"网安\",\"collapsed\":false,\"items\":[{\"text\":\"网络安全\",\"link\":\"/其他/八股/其他/网安/网络安全\"}]}]}]},{\"text\":\"项目\",\"collapsed\":false,\"items\":[{\"text\":\"项目部署\",\"collapsed\":false,\"items\":[{\"text\":\"web\",\"link\":\"/其他/项目/项目部署/web\"},{\"text\":\"vitepress\",\"collapsed\":false,\"items\":[{\"text\":\"GithubPages部署\",\"link\":\"/其他/项目/项目部署/vitepress/GithubPages部署\"},{\"text\":\"netlify部署\",\"link\":\"/其他/项目/项目部署/vitepress/netlify部署\"},{\"text\":\"Nginx部署\",\"link\":\"/其他/项目/项目部署/vitepress/Nginx部署\"}]}]}]}],\"/LLM/\":[{\"text\":\"LangChain\",\"collapsed\":false,\"items\":[{\"text\":\"quick_start\",\"link\":\"/LLM/LangChain/quick_start\"},{\"text\":\"Prompt\",\"link\":\"/LLM/LangChain/Prompt\"},{\"text\":\"Model\",\"link\":\"/LLM/LangChain/Model\"},{\"text\":\"OutputParser\",\"link\":\"/LLM/LangChain/OutputParser\"},{\"text\":\"LCEL表达式与Runnable可运行协议\",\"link\":\"/LLM/LangChain/LCEL表达式与Runnable可运行协议\"}]}],\"/环境搭建/\":[{\"text\":\"docker\",\"collapsed\":false,\"items\":[{\"text\":\"mysql\",\"link\":\"/环境搭建/docker/mysql\"}]}]}},\"locales\":{},\"scrollOffset\":134,\"cleanUrls\":false}");</script>
    
  </body>
</html>